// pages/notice/notice.js
var app = getApp();
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabData: [
      { txt: '交易物流', current: false },
      // { txt: '通知', current: false },
      { txt: '留言', current: false }
    ],
    noticeData:[],
    leaveMessage:[],
    userInfo: '',
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData0: true,
    hasData1: true,
    page0: 1,
    page1: 1,
    current:0,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.tabData[parseInt(options.index)].current=true;
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      tabData: this.data.tabData,
      current: parseInt(options.index)
    })
    if (options.index=='0'){
      this.getNoticeInfo(true);
      wx.setNavigationBarTitle({
        title: '交易物流',
      })
    }else{
      this.getLeaveData();
      wx.setNavigationBarTitle({
        title: '留言',
      })
    }
    wx.showLoading();
  },
  // 获取系统通知
  getNoticeInfo: function (flag){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'shopOrder/pageLog',
      data: {
        orderStatus:'3',
        userId: this.data.userInfo.id
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData0: false
          })
        } else {
          _this.setData({
            hasData0: true
          })
        }
        res.data.data.pp.forEach(function(ele){
          ele.timeStr = util.formatTime(ele.updateTime);
          ele.shopThing.realThingBanerImgArr = ele.shopThing.realThingBanerImg.split(',');
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            noticeData: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.noticeData = _this.data.noticeData.concat(res.data.data.pp);
        _this.setData({
          noticeData: _this.data.noticeData
        })
      }
    })
  },
  getLeaveData(flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'messageInfo/page', //仅为示例，并非真实的接口地址
      data: {
        otherSideId: this.data.userInfo.id,
        currentPage: this.data.page1,
        pageNumber: '10',
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (res.data.data.pp.length < 10) {
          _this.setData({
            hasData1: false
          })
        } else {
          _this.setData({
            hasData1: true
          })
        }
        res.data.data.pp.forEach(function (ele) {
          if (ele.createTime) {
            ele.time = util.getDays(ele.createTime);
            // ele.time = util.getDays(new Date(ele.time.replace(/-/g, "/")).getTime());
          }
          if (ele.messageInfos != undefined && ele.messageInfos.length > 0) {
            ele.messageInfos.forEach(function (item) {
              if (item.createTime) {
                item.time = util.getDays(item.createTime);
              }
            })
          }
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          _this.setData({
            leaveMessage: []
          });
        } else {
          _this.setData({
            loadMore: false
          });
        }
        _this.data.leaveMessage = _this.data.leaveMessage.concat(res.data.data.pp);
        _this.setData({
          leaveMessage: _this.data.leaveMessage
        })
      }
    })
  },
  swichTab: function (e) {
    this.data.tabData.forEach(function (ele) {
      ele.current = false;
    })
    this.data.tabData[e.currentTarget.dataset.value].current = true;
    this.setData({
      tabData: this.data.tabData,
      current: e.currentTarget.dataset.value
    })
  },
  showLeave:function(e){
    var item = e.currentTarget.dataset.obj;
    if (item.infoId){
      wx.navigateTo({
        url: "/pages/leaveWord/leaveWord?id=" + item.infoId + '&status=' + item.status + '&userId=' + item.userId + '&pulishId=' + item.otherSideId + '&backupA=' + item.backupA
      })
    }else{
      wx.navigateTo({
        url: "/pages/leaveWord/leaveWord?id=" + item.userInfo.id + '&status=' + item.status + '&userId=' + item.userId + '&pulishId=' + item.otherSideId + '&backupA=' + item.backupA
      })
    }  
  },
  jumpUrl: function (e) {
    var item = e.currentTarget.dataset.obj
    wx.navigateTo({
      url: "/pages/logistics/logistics?number=" + item.orderLogistics.logNum
    }) 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.setData({
      ['page' + this.data.current]: 1
    })
    if (this.data.current == 0) {
      this.getNoticeInfo(true);
    } else {
      this.getLeaveData(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data['hasData' + this.data.current]) {
      this.setData({
        loadMoreTxt: '正在加载数据。。。',
        loadMore: true,
        ['page' + this.data.current]: this.data['page' + this.data.current] + 1
      });
      if (this.data.current == 0) {
        this.getNoticeInfo();
      } else {
        
        this.getLeaveData();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})